/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:43
 **/




#include <iostream>
#include <CourseMapper.h>
#include <StudentMapper.h>
#include "CourseSelectionService.h"
#include "CourseSelectionMapper.h"
#include "vector"

//todo service层 临时容器 换用vector

using namespace std;


int CourseSelectionService::showCourseSelectionByClass(std::string classId) {
    StudentMapper studentMapper;
    CourseSelectionMapper courseSelectionMapper;
    vector<Student> studentList =  studentMapper.selectStudentByClass(classId);
    vector<CourseSelection> courseSelectionList;
    for(Student student:studentList){
        vector<CourseSelection> onceCourseSelectionList =  courseSelectionMapper.selectByStudentId(student.getId());
        for(CourseSelection courseSelection:onceCourseSelectionList){
            courseSelectionList.push_back(courseSelection);
        }
    }
    this->showCourseSelectionList(courseSelectionList);
    cout<<"共"<<courseSelectionList.size()<<"条记录."<<endl;
    return 0;
}


int CourseSelectionService::showAllCourseSelectionList() {
    CourseSelectionMapper courseSelectionMapper;
    list<CourseSelection> courseSelectionList = courseSelectionMapper.getCourseSelectionList();
    return this->showCourseSelectionList(courseSelectionList);

}

int CourseSelectionService::batchInsertCourseSelection() {
    CourseSelectionMapper courseSelectionMapper;
    StudentMapper studentMapper;
    CourseMapper courseMapper;
    list<CourseSelection> courseSelectionList = courseSelectionMapper.getCourseSelectionList();
    int onceCommitChoice = -1;
    list<CourseSelection> tempCourseSelectionList;
    while(true){
        string field,studentId,courseId;
        cout<<"请输入学生学号(12位统一编号):";
        cin>>studentId;
        int studentExist = studentMapper.checkId(studentId);
        if(!studentExist){
            cout<<"找不到该学生，可能该学生编号暂未录入到系统"<<endl;
            if(isContinue(courseSelectionList,tempCourseSelectionList)){
                continue;
            }
            return 0;
        }
        cout<<"请输入课程编号(6位统一编号)";
        cin>>courseId;
        int courseExist = courseMapper.checkId(courseId);
        if(!courseExist){
            cout<<"找不到该课程，可能该课程编号暂未录入到系统"<<endl;
            if(isContinue(courseSelectionList,tempCourseSelectionList)){
                continue;
            }
            return 0;
        }

        CourseSelection* courseSelection = new CourseSelection();
        courseSelection->setStudentId(studentId);
        courseSelection->setCourseId(courseId);
        cout<<"请输入课程学年(请输入前半年 如，2020~2021学年 输入2020)：";
        cin>>field;
        courseSelection->setSchoolYear(stoi(field));
        cout<<"请输入课程所在学期(上半年输入1，下半年输入2)：";
        cin>>field;
        courseSelection->setSemester(stof(field));
        cout<<"请输入该学生是否是重修选课(否请输入 false ,是请输入 true )";
        cin>>field;
        bool isRelearn;
        field=="false"?isRelearn= false:isRelearn= true;
        isRelearn?courseSelection->setIsRelearn(true):courseSelection->setIsRelearn(false);
        int selectionIsRepeated = this->checkSelection(studentId,courseId,isRelearn, tempCourseSelectionList);
        if(selectionIsRepeated){
            cout<<"学生选课情况有重复！请重试"<<endl<<endl;
            if(isContinue(courseSelectionList,tempCourseSelectionList)){
                continue;
            }
            return 0;
        }

        cout<<endl<<"您输入的选课信息是："<<endl;
        cout<<"学生学号\t课程编号\t学年\t学期\t是否重修"<<endl;
        cout<<courseSelection->getStudentId()<<" "<<courseSelection->getCourseId()<<" ";
        cout<<courseSelection->getSchoolYear()<<" "<<courseSelection->getSemester()<<" ";
        courseSelection->isRelearn()==0?cout<<"false"<<endl:cout<<"true"<<endl;
        cout<<endl<<"确认该条记录正确吗?(1.正确，加入到暂存区  2.错误，重新输入  0.放弃输入，退回上一页面)";
        cin>>onceCommitChoice;
        if(onceCommitChoice==0){
            cout<<"退出成功";
            system("pause");
            system("cls");
            return 0;
        }
        else if(onceCommitChoice==2){
            continue;
        }
        tempCourseSelectionList.push_back(*courseSelection);
        cout<<endl<<"当前已暂存的信息："<<endl;
        cout<<"学生学号\t课程编号\t学年\t学期\t是否重修"<<endl;
        for(CourseSelection courseSelection:tempCourseSelectionList){
            cout<<courseSelection.getStudentId()<<" "<<courseSelection.getCourseId()<<" ";
            cout<<courseSelection.getSchoolYear()<<" "<<courseSelection.getSemester()<<" ";
            courseSelection.isRelearn()==0?cout<<"false"<<endl:cout<<"true"<<endl;
        }
        if(isContinue(courseSelectionList,tempCourseSelectionList)){
            continue;
        }
        return 0;



    }

}

int CourseSelectionService::isContinue(list<CourseSelection>& courseSelectionList,list<CourseSelection>& tempCourseSelectionList){
    int continueChoice,saveResultChoice;
    cout<<endl<<"是否继续录入？(1.继续录入 0.结束录入)";
    cin>>continueChoice;
    if(continueChoice==1){
        return 1;
//       returns to continue;
    }
    else if(continueChoice==0){
        cout<<"是否保存结果信息？(1.保存 0.放弃，本次录入将不被保存)"<<endl;
        cin>>saveResultChoice;
        if(saveResultChoice){
            for(CourseSelection courseSelection:tempCourseSelectionList){
                courseSelectionList.push_back(courseSelection);
            }
            CourseSelectionMapper courseSelectionMapper;
            courseSelectionMapper.setCourseSelectionList(courseSelectionList);
            courseSelectionMapper.batchInsertCourseSelection(courseSelectionList);
            cout<<"保存成功！"<<endl;
        }
        else{
            cout<<"退出成功。"<<endl;
        }
        system("pause");
        system("cls");
        return 0;
    }
    cout<<"出错啦！回到上一页面"<<endl;
    return 0;

}



int CourseSelectionService::checkSelection(string studentId, string courseId, bool isRelearn, list<CourseSelection>& tempCourseSelectionList) {
    CourseSelectionMapper courseSelectionMapper;
    list<CourseSelection> courseSelectionList = courseSelectionMapper.getCourseSelectionList();
    for(CourseSelection courseSelection:courseSelectionList){
        if(courseSelection.getStudentId()==studentId&&courseSelection.getCourseId()==courseId
        && courseSelection.isRelearn()==isRelearn){
            return 1;
        }
    }
    for(CourseSelection courseSelection:tempCourseSelectionList){
        if(courseSelection.getStudentId()==studentId&&courseSelection.getCourseId()==courseId){
            return 1;
        }
    }

    return 0;
}

int CourseSelectionService::showCourseSelectionList(list<CourseSelection> &courseSelectionList) {
    for(CourseSelection courseSelection:courseSelectionList){
        cout<<courseSelection.getStudentId()<<" ";
        cout<<courseSelection.getCourseId()<<" ";
        cout<<courseSelection.getSchoolYear()<<" ";
        cout<<courseSelection.getSemester()<<" ";
        courseSelection.isRelearn()==0?cout<<"false"<<endl:cout<<"true"<<endl;
    }
    cout<<"共"<<courseSelectionList.size()<<"条记录."<<endl;
    return 0;
}
int CourseSelectionService::showCourseSelectionList(vector<CourseSelection> &courseSelectionList) {
    for(CourseSelection courseSelection:courseSelectionList){
        cout<<courseSelection.getStudentId()<<" ";
        cout<<courseSelection.getCourseId()<<" ";
        cout<<courseSelection.getSchoolYear()<<" ";
        cout<<courseSelection.getSemester()<<" ";
        courseSelection.isRelearn()==0?cout<<"false"<<endl:cout<<"true"<<endl;
    }
    cout<<"共"<<courseSelectionList.size()<<"条记录."<<endl;
    return 0;
}


CourseSelectionService::CourseSelectionService() {

}

CourseSelectionService::~CourseSelectionService() {}


